import Toast from 'react-native-toast-message';
import { Platform } from 'react-native';
import dayjs from 'dayjs'
import { Buffer } from 'buffer'

const BaseTools = {

    /**
     * * 公共处理 Promise，first-err 模式
     *
     * @param {Promise<T>} promise
     * @return {Promise<any>}
     */
    to: (promise) => promise.then(res => [undefined, res]).catch(err => [err, undefined]),

    /**
     * * 公共消息处理
     *
     * @param {object|string} e
     * @return {string}
     */
    showMessage: (e) => {
        if (e instanceof Object) {
            return (e?.message || e?.msg || '') + (e.code ? `(${e.code})` : '')
        }
        return "~服务器异常~";
    },

    /**
      * * 请求公共
      *
      * @param {string|{msg?:string,message?:string}} err
      * @param {number} time
      * @param {Function} call
      */
    showInfo: (msg, time = 2, call = undefined) => {
        const str = typeof msg === 'string' ? msg : BaseTools.showMessage(msg)
        Toast.show({
            type: 'info',
            text1: str,
            text2: '🚧   温馨提醒',
            text2Style: { marginTop: 5 },
            visibilityTime: time * 1000,
            onHide: call?.(),
        });
    },
    showSuccess: (msg, time = 2, call = undefined) => {
        const str = typeof msg === 'string' ? msg : BaseTools.showMessage(msg)
        Toast.show({
            type: 'success',
            text1: str,
            text2: '🚀   成功提醒',
            text2Style: { marginTop: 5 },
            visibilityTime: time * 1000,
            onHide: call?.(),
        });
    },
    showError: (msg, time = 2, call = undefined) => {
        const str = typeof msg === 'string' ? msg : BaseTools.showMessage(msg)
        Toast.show({
            type: 'error',
            text1: str,
            text2: '🚨   错误提醒',
            text2Style: { marginTop: 5 },
            visibilityTime: time * 1000,
            onHide: call?.(),
        });
    },

    isIOS: () => Platform.OS === 'ios',

    isAndroid: () => Platform.OS === 'android',

    /**
     * * 解析 Base64
     *
     * @param {string} result
     * @returns {Promise<[any,string|undefined]>}
     */
    decodeBase64: async (result) => {
        try {
            let str = JSON.parse(new Buffer(result, 'base64'))
            return ([undefined, str])
        } catch (e) {
            return ([e, undefined])
        }
    },

    /**
     * * 转换 Base64
     * @param {string} str 字符串
     * @returns {string}
     */
    encodeBase64: (str) => Buffer.from(str, 'utf8').toString('base64'),

    /**
     * * dayjs 对象，解决页面需要引入问题
     *
     * @param {any[]} arg 剩余参数
     * @returns {dayjs.Dayjs}
     */
    dayjs,

    /**
     * * 获取距今 num 日期
     *
     * @param {number} num 天数
     * @param {'hours' | 'days' | 'months' | 'years'='days'} type 类型
     * @param {string='YYYY-MM-DD HH:mm:ss'} format 格式化
     * @returns {string}
     */
    getDateByBeforeNow: (num, type = 'days', format = 'YYYY-MM-DD HH:mm:ss') => dayjs().subtract(num, type).format(format),

    /**
     * * 获取之后 num 日期
     *
     * @param {number} num 天数
     * @param {'hours' | 'days' | 'months' | 'years'='days'} type 类型
     * @param {string='YYYY-MM-DD HH:mm:ss'} format 格式化
     * @returns {string}
     */
    getDateByAfterNow: (num, type = 'days', format = 'YYYY-MM-DD HH:mm:ss') => dayjs().add(num, type).format(format),
}

export default BaseTools